import 'package:flutter/material.dart';
import 'package:jump_scape/js_data/js_event.dart';
import 'package:jump_scape/js_ui/js_activity/js_home.dart';
import 'package:jump_scape/js_ui/js_activity/js_login.dart';
import 'package:jump_scape/js_ui/js_widget/js_image_view.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:jump_scape/js_ui/js_widget/js_style.dart';

import '../../js_data/js_box/js_init.dart';
import '../../js_data/js_common.dart';
import '../../js_data/js_login.dart';

class JsLaunchActivity extends StatefulWidget {
  static const url = 'launch';
  const JsLaunchActivity({super.key});

  @override
  State<StatefulWidget> createState() => _JsLaunchActivity();
}

class _JsLaunchActivity extends State<JsLaunchActivity> {
  @override
  void initState() {
    super.initState();
    _init();
  }

  _init() async {
    await JsObjectBox.create();
    await JsObjectBox.setting;

    JsEventManager.statPage(page: JsEventName.jsNameLaunch);

    final isValidToken = await JsLogin().isValidToken();
    if (!isValidToken) JsLogin().logout(onFailed: (String msg) {}, onSuccess: () {});
    JsCommon().getAppConfig();
    if (mounted) {
      if (isValidToken) {
        Navigator.popAndPushNamed(context, JsHomeActivity.url);
      } else {
        Navigator.popAndPushNamed(context, JsLoginActivity.url);
      }
    }
  }

  @override
  Widget build(BuildContext context) {
    return WillPopScope(
      onWillPop: () => Future.value(false),
      child: Scaffold(
        body: Stack(
          children: [
            jsImage('assets/image/ic_login_bg.png', width: double.infinity, height: double.infinity, fit: BoxFit.cover),
            Align(
              alignment: Alignment.topCenter,
              child: Container(
                width: 100.r,
                height: 100.r,
                clipBehavior: Clip.antiAlias,
                margin: EdgeInsets.only(top: 80.r),
                decoration: jsShapeRound(radius: 10.r),
                child: jsImage('assets/image/ic_logo.png'),
              ),
            ),
          ],
        ),
      ),
    );
  }
}
